package com.bls.dao.impl;

import com.bls.dao.DraftDao;
import com.bls.entity.Draft;
import com.bls.entity.query.DraftQuery;
import com.bls.entity.vo.DraftVo;
import com.fy.toolhelper.db.BaseDaoImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoliang
 * @date 2022/01/10 20:12
 **/
public class DraftDaoImpl extends BaseDaoImpl<Draft> implements DraftDao {

    public DraftDaoImpl() throws Exception {
    }

    @Override
    public List<DraftVo> pageConditionDraftList(Connection connection, DraftQuery draftQuery, Integer pageSize, Integer current) throws SQLException {

        int start = pageSize * (current - 1);
        StringBuilder sql = new StringBuilder("SELECT * FROM bls_draft WHERE 1 = 1 ");

        if (!StringUtils.isEmpty(draftQuery.getName())) {
            sql.append(" and name like '%").append(draftQuery.getName()).append("%'");
        }
        if (!StringUtils.isEmpty(draftQuery.getBandId())) {
            sql.append(" and band_id = ").append(draftQuery.getBandId());
        }
        if (!StringUtils.isEmpty(draftQuery.getCreatorId())) {
            sql.append(" and creator_id = ").append(draftQuery.getCreatorId());
        }
        if (!StringUtils.isEmpty(draftQuery.getDeveloped())) {
            sql.append(" and is_developed = ").append(draftQuery.getDeveloped());
        }
        if (!StringUtils.isEmpty(draftQuery.getShared())) {
            sql.append(" and is_shared = ").append(draftQuery.getShared());
        }
        sql.append(" limit ? , ?");

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        pstm.setInt(1, start);
        pstm.setInt(2, pageSize);
        ResultSet rs = pstm.executeQuery();
        List<DraftVo> draftVoList = new ArrayList<>();
        while (rs.next()) {
            DraftVo draftVo = new DraftVo();
            Draft draftEntity = getDraftEntity(rs);
            BeanUtils.copyProperties(draftEntity, draftVo);
            draftVoList.add(draftVo);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return draftVoList;
    }

    @Override
    public int pageConditionDraftTotal(Connection connection, DraftQuery draftQuery) throws SQLException {

        StringBuilder sql = new StringBuilder("SELECT COUNT(*) as count FROM bls_draft WHERE 1 = 1 ");

        if (!StringUtils.isEmpty(draftQuery.getName())) {
            sql.append(" and name like '%").append(draftQuery.getName()).append("%'");
        }
        if (!StringUtils.isEmpty(draftQuery.getBandId())) {
            sql.append(" and band_id = ").append(draftQuery.getBandId());
        }
        if (!StringUtils.isEmpty(draftQuery.getCreatorId())) {
            sql.append(" and creator_id = ").append(draftQuery.getCreatorId());
        }
        if (!StringUtils.isEmpty(draftQuery.getDeveloped())) {
            sql.append(" and is_developed = ").append(draftQuery.getDeveloped());
        }
        if (!StringUtils.isEmpty(draftQuery.getShared())) {
            sql.append(" and is_shared = ").append(draftQuery.getShared());
        }

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        ResultSet rs = pstm.executeQuery();
        int count = -1;
        while (rs.next()) {
            count = rs.getInt("count");
        }
        System.out.println(sql.toString());
        System.out.println(count);
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return count;
    }

    @Override
    public int deleteDraft(Connection connection, Long draftId) throws SQLException {

        String sql = "DELETE FROM bls_draft WHERE id = ?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setLong(1, draftId);
        int i = pstm.executeUpdate();
        if (pstm != null) {
            pstm.close();
        }
        return i;
    }

    @Override
    public Draft selectDraftById(Connection connection, Long draftId) throws SQLException {
        String sql = "SELECT * FROM bls_draft WHERE id = ?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setLong(1, draftId);
        ResultSet rs = pstm.executeQuery();
        Draft draft = null;
        while (rs.next()) {
            draft = getDraftEntity(rs);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return draft;
    }

    @Override
    public List<DraftVo> pageConditionAnalyseRunningStatusList(Connection connection, DraftQuery draftQuery, Integer pageSize, Integer current) throws SQLException {

        int start = pageSize * (current - 1);
        StringBuilder sql = new StringBuilder("SELECT d.*,a.`status`,a.`code` FROM bls_draft d LEFT JOIN bls_analyse a ON d.analyse_id = a.id WHERE 1=1 ");

        if(!StringUtils.isEmpty(draftQuery.getId())){
            sql.append(" and d.id = ").append(draftQuery.getId());
        }
        if (!StringUtils.isEmpty(draftQuery.getStatus())) {
            sql.append(" and status = ").append(draftQuery.getStatus());
        }
        if (!StringUtils.isEmpty(draftQuery.getName())) {
            sql.append(" and name like '%").append(draftQuery.getName()).append("%'");
        }
        if (!StringUtils.isEmpty(draftQuery.getBandId())) {
            sql.append(" and d.band_id = ").append(draftQuery.getBandId());
        }
        if (!StringUtils.isEmpty(draftQuery.getCreatorId())) {
            sql.append(" and creator_id = ").append(draftQuery.getCreatorId());
        }
        if (!StringUtils.isEmpty(draftQuery.getDeveloped())) {
            sql.append(" and is_developed = ").append(draftQuery.getDeveloped());
        }
        if (!StringUtils.isEmpty(draftQuery.getShared())) {
            sql.append(" and is_shared = ").append(draftQuery.getShared());
        }
        sql.append(" limit ? , ?");

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        pstm.setInt(1, start);
        pstm.setInt(2, pageSize);
        ResultSet rs = pstm.executeQuery();
        List<DraftVo> draftVoList = new ArrayList<>();
        while (rs.next()) {
            DraftVo draftVo = new DraftVo();
            Draft draftEntity = getDraftEntity(rs);
            BeanUtils.copyProperties(draftEntity, draftVo);
            draftVo.setCode(rs.getString("code"));
            draftVo.setStatus(rs.getInt("status"));
            draftVoList.add(draftVo);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return draftVoList;


    }

    @Override
    public int pageConditionAnalyseRunningStatusTotal(Connection connection, DraftQuery draftQuery) throws SQLException {

        StringBuilder sql = new StringBuilder("SELECT COUNT(*) as count FROM bls_draft d LEFT JOIN bls_analyse a ON d.analyse_id = a.id WHERE 1=1 ");

        if(!StringUtils.isEmpty(draftQuery.getId())){
            sql.append(" and d.id = ").append(draftQuery.getId());
        }
        if (!StringUtils.isEmpty(draftQuery.getStatus())) {
            sql.append(" and status = ").append(draftQuery.getStatus());
        }
        if (!StringUtils.isEmpty(draftQuery.getName())) {
            sql.append(" and name like '%").append(draftQuery.getName()).append("%'");
        }
        if (!StringUtils.isEmpty(draftQuery.getBandId())) {
            sql.append(" and d.band_id = ").append(draftQuery.getBandId());
        }
        if (!StringUtils.isEmpty(draftQuery.getCreatorId())) {
            sql.append(" and creator_id = ").append(draftQuery.getCreatorId());
        }
        if (!StringUtils.isEmpty(draftQuery.getDeveloped())) {
            sql.append(" and is_developed = ").append(draftQuery.getDeveloped());
        }
        if (!StringUtils.isEmpty(draftQuery.getShared())) {
            sql.append(" and is_shared = ").append(draftQuery.getShared());
        }

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        ResultSet rs = pstm.executeQuery();
        int count = -1;
        while (rs.next()) {
            count = rs.getInt("count");
        }
        System.out.println(sql.toString());
        System.out.println(count);
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return count;


    }

    private Draft getDraftEntity(ResultSet rs) throws SQLException {
        Draft draft = new Draft();
        draft.setId(rs.getLong("id"));
        draft.setAnalyseId(rs.getLong("analyse_id"));
        draft.setBandId(rs.getLong("band_id"));
        draft.setBandName(rs.getString("band_name"));
        draft.setCreatorId(rs.getLong("creator_id"));
        draft.setCreatorName(rs.getString("creator_name"));
        draft.setName(rs.getString("name"));
        draft.setContent(rs.getString("content"));
        draft.setDescription(rs.getString("description"));
        draft.setDeveloped(rs.getInt("is_developed"));
        draft.setShared(rs.getInt("is_shared"));
        draft.setCreateTime(rs.getTimestamp("create_time"));
        draft.setUpdateTime(rs.getTimestamp("update_time"));
        return draft;
    }
}
